﻿using Sidvall.Serialization;
using System.Collections.Generic;
using System.Linq;

namespace Sidvall.Data
{
    public class RelationItemCollection : System.Collections.ObjectModel.Collection<RelationItem>
    {
        #region Public Members

        #region Add

        public RelationItem Add(string sourceAlias, string relatedTableName)
        {
            return Add(sourceAlias, relatedTableName, RelationType.Inner);
        }
        public RelationItem Add(string sourceAlias, string relatedTableName, RelationType relationType)
        {
            return Add(sourceAlias, relatedTableName, relationType, null, null);
        }
        public RelationItem Add(string sourceAlias, string relatedTableName, RelationType relationType, string sourceFieldName, string relatedFieldName)
        {
            var item = new RelationItem()
            {
                SourceAlias = sourceAlias,
                RelatedTableName = relatedTableName,
                RelationType = relationType,
            };
            if (!string.IsNullOrWhiteSpace(sourceFieldName))
                item.Fields.Add(sourceFieldName, relatedFieldName);
            Add(item);
            return item;
        }

        #endregion
        #region AddExpression

        public RelationItem AddExpression(string sourceAlias, string relatedExpression, string relatedAlias)
        {
            return AddExpression(sourceAlias, relatedExpression, relatedAlias, RelationType.Inner);
        }
        public RelationItem AddExpression(string sourceAlias, string relatedExpression, string relatedAlias, RelationType relationType)
        {
            return AddExpression(sourceAlias, relatedExpression, relatedAlias, relationType, null, null);
        }
        public RelationItem AddExpression(string sourceAlias, string relatedExpression, string relatedAlias, RelationType relationType, string sourceFieldName, string relatedFieldName)
        {
            var item = new RelationItem()
            {
                SourceAlias = sourceAlias,
                RelatedExpression = relatedExpression,
                RelatedAlias = relatedAlias,
                RelationType = relationType,
            };
            if (!string.IsNullOrWhiteSpace(sourceFieldName))
                item.Fields.Add(sourceFieldName, relatedFieldName);
            Add(item);
            return item;
        }

        #endregion
        #region Contains

        public bool Contains(string sourceAlias, string relatedAlias)
        {
            var items = Find(sourceAlias, relatedAlias);
            return items.Any();
        }

        #endregion
        #region Find

        public IEnumerable<RelationItem> Find(string sourceAlias, string relatedAlias)
        {
            return (from o in this
                    where o.SourceAlias.Equals(sourceAlias, true, System.StringComparison.OrdinalIgnoreCase) &&
                        o.RelatedAliasResolved.Equals(relatedAlias, true, System.StringComparison.OrdinalIgnoreCase)
                    select o);
        }

        #endregion

        #endregion
        #region Constructors

        public RelationItemCollection()
        {
        }

        #endregion
    }
}
